package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectAll();
        List<NursingPlanVo> voList = BeanUtil.copyToList(nursingPlans, NursingPlanVo.class);
        return voList;
    }

    /**
     * 新增护理计划
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(NursingPlanDto dto) {
        //判断名称是否重复
        Integer count = nursingPlanMapper.selectNameCount(dto.getPlanName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPPLAN_NAME_EXICT);
        }
        //新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.insert(nursingPlan);
        Long NurId = nursingPlan.getId();
        //新增护理计划和项目关联表
        if (BeanUtil.isNotEmpty(dto.getProjectPlans())) {
            List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
            List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
            nursingProjectPlans.forEach((item) -> {
                item.setPlanId(NurId);
            });
            nursingProjectPlanMapper.insertNursingProjectPlan(nursingProjectPlans);
        }
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        //调用mapper
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.pageQuery(name, status);

        return PageResponse.of(page, NursingPlanVo.class);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        //查询plan表
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo planVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        //查询中间表
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByNurPlanId(id);
        if (BeanUtil.isNotEmpty(list)) {
            planVo.setProjectPlans(list);
        }
        return planVo;
    }

    /**
     * 修改护理计划
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlan(NursingPlanDto dto) {
        //判断名称是否重复
        Integer count = nursingPlanMapper.selectNameCountExcludeId(dto.getPlanName(), dto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPPLAN_NAME_EXICT);
        }
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);
        if (BeanUtil.isNotEmpty(dto.getProjectPlans())) {
            //存在关联表数据 先全删再新增
            nursingProjectPlanMapper.deleteByNurPlanId(dto.getId());
            List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
            //给中间表的plan_id赋值
            projectPlans.forEach((item) -> {
                item.setPlanId(dto.getId());
            });
            List<NursingProjectPlan> nppList = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
            nursingProjectPlanMapper.insertNursingProjectPlan(nppList);
        }
    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingPlan(Long id) {
        //判断该护理计划是否关联的有护理等级
        Integer count = nursingPlanMapper.selectCountById(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPLAN_LEVEL_EXICT);
        }
        nursingPlanMapper.deleteById(id);
        //查询该护理计划是否关联的有中间表数据
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByNurPlanId(id);
        if (BeanUtil.isNotEmpty(list)) {
            nursingProjectPlanMapper.deleteByNurPlanId(id);
        }
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }


}
